package com.lupan.tomcatStudy.mongodb.districtToBounds;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.geo.Point;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明：省，城市名称转换为区域范围的工具类
 *
 * @author lupan
 * @version 2016/4/27 17:08
 */
public class ConvertUtil {


    /**
     * 将区域名字转化为区域范围边界点字符串
     * @param name 区域名
     * @param level 区域级别
     * @return 区域范围边界点字符串
     */
    public static JSONObject convert(String name,String level){
        String urlStr = null;

        try {
            urlStr = "http://restapi.amap.com/v3/config/district?subdistrict=0&extensions=all&level="+ URLEncoder.encode(level, "utf-8") +"&key=7de8697669288fc848e12a08f58d995e&s=rsv3&output=json&keywords="+URLEncoder.encode(name, "utf-8")+"&platform=JS&logversion=2.0&sdkversion=1.3&appname=http%3A%2F%2Flocalhost%3A63342%2Fhello-world%2Findex.html&csid=0DE4DC6F-8A8C-43FB-9ACD-BCD7AAEF3A95";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader=null;
        try {
            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection();
            // 设置通用请求属性
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("Connection","Keep-Alive");
            conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64)");
            // 建立连接
            conn.connect();
            //读取url响应
            bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            String response = "";
            while ((line = bufferedReader.readLine()) != null){
                response += "\n"+line;
            }
            //打印数据到控制台
            System.out.println(String.format("正在转换：%s",name));
            System.out.print("接口响应："+response);
            return JSON.parseObject(response);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    /**
     * 格式化边界点
     * @param boundsStr 边界点字符串
     * @return 边界点list
     */
    public static List<Point> formatBounds(String boundsStr) {
        List<Point> points = new ArrayList<>();

        String[] pointStrs = boundsStr.split(";");
        for (String pointStr : pointStrs){
            points.add(new Point(Double.parseDouble(pointStr.split(",")[0]),Double.parseDouble(pointStr.split(",")[1])));
        }
        //将第一个point添加到末尾
        String firstPointStr = pointStrs[0];
        points.add(new Point(Double.parseDouble(firstPointStr.split(",")[0]),Double.parseDouble(firstPointStr.split(",")[1])));
        return points;
    }
}
